﻿using Newtonsoft.Json;
using System;

namespace AutomaticGrab.Commlibs.Extensions
{
    public static class ObjectExtension
    {
        public static string ObjectToJson(this object data, bool needFormat = false)
        {
            try
            {
                return needFormat ?
                    JsonConvert.SerializeObject(data, Formatting.Indented) :
                    JsonConvert.SerializeObject(data);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return "";
            }
        }


        public static T JsonToObject<T>(this string json)
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(json);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return default(T);
            }
        }

        public static T ConvertToNewObject<T>(this object data)
        {
            try
            {
                if (data == null) return default(T);
                return data.ObjectToJson()
                    .JsonToObject<T>();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return default(T);
            }
        }

        public static int ToInt32<T>(this T number) where T : IConvertible
        {
            try
            {
                return (int)ToDecimal(number);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return 0;
            }
        }
        public static decimal ToDecimal<T>(this T number) where T : IConvertible
        {
            try
            {
                if (number == null)
                {
                    return 0;
                }
                return Convert.ToDecimal(number);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return 0;
            }
        }

        public static decimal ToDecimal(this object number)
        {
            try
            {
                if (number == null)
                {
                    return 0;
                }
                return Convert.ToDecimal(number);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return 0;
            }
        }

        public static int ToInt32(this object number)
        {
            try
            {
                return (int)ToDecimal(number);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return 0;
            }
        }

        public static DateTime ToDateTime<T>(this T number) where T : IConvertible
        {
            try
            {
                if (number == null)
                {
                    return default(DateTime);
                }
                return Convert.ToDateTime(number);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return default(DateTime);
            }
        }
    }
}
